package com.feng.common.utils;


import com.feng.common.enums.CommonErrorCode;
import com.feng.common.enums.ErrorCode;
import com.feng.common.exception.BizException;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

@UtilityClass
public class AssertUtils {

    /**
     * 字符串空断言
     *
     * @param text    被校验字符串
     * @param message 断言消息
     */
    public static void hasText(String text, String message) {
        if (StringUtils.isBlank(text)) {
            throw new BizException(CommonErrorCode.APP_BIZ_ERROR.getCode(), message);
        }
    }

    /**
     * 空对象断言
     *
     * @param object  被校验的对象
     * @param message 断言失败返回的错误消息
     */
    public static void isNull(Object object, String message) {
        if (null != object) {
            throw new BizException(CommonErrorCode.APP_BIZ_ERROR.getCode(), message);
        }
    }

    /**
     * 空对象断言
     */
    public static void isNull(Object object, ErrorCode errorCode) {
        if (null != object) {
            throw new BizException(errorCode.getCode(), errorCode.getMsg());
        }
    }

    /**
     * 非空对象断言
     *
     * @param object  被校验的对象
     * @param message 断言失败返回的错误消息
     */
    public static void isNotNull(Object object, String message) {
        if (null == object) {
            throw new BizException(CommonErrorCode.APP_BIZ_ERROR.getCode(), message);
        }
    }

    /**
     * 非空对象断言
     */
    public static void isNotNull(Object object, ErrorCode errorCode) {
        if (null == object) {
            throw new BizException(errorCode.getCode(), errorCode.getMsg());
        }
    }

    public static void isTrue(boolean expression, ErrorCode errorCode) {
        if (!expression) {
            throw new BizException(errorCode.getCode(), errorCode.getMsg());
        }
    }

    public static void isTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new BizException(CommonErrorCode.APP_BIZ_ERROR.getCode(), errorMsg);
        }
    }

    public static void isFalse(boolean expression, ErrorCode errorCode) {
        if (expression) {
            throw new BizException(errorCode.getCode(), errorCode.getMsg());
        }
    }

    public static void isFalse(boolean expression, String errorMsg) {
        if (expression) {
            throw new BizException(CommonErrorCode.APP_BIZ_ERROR.getCode(), errorMsg);
        }
    }
}
